/**
 * Title :	jFamilyTree generator
 * Description: jFamilyTree generator
 * Version:	provider
 * Copyright (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package jFamilyTree.generator.tree;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

import jFamilyTree.Messages;
import jFamilyTree.model.Edge;
import jFamilyTree.model.MenuItem;
import jFamilyTree.model.Node;
import jFamilyTree.model.TreeMutableEdge;
import jFamilyTree.model.TreeMutableNode;

/**
 * XML tree renderer
 * 
 * @author Bernard Bou
 */
public class Renderer extends DefaultTreeCellRenderer
{
	private static final long serialVersionUID = 1L;

	// icons to use

	/**
	 * jFamilyTree icon
	 */
	static protected final ImageIcon thejFamilyTreeIcon;

	/**
	 * Tree icon
	 */
	static protected final ImageIcon theTreeIcon;

	/**
	 * Nodes icon
	 */
	static protected final ImageIcon theNodesIcon;

	/**
	 * Root icon
	 */
	static protected final ImageIcon theRootIcon;

	/**
	 * Node icon
	 */
	static protected final ImageIcon theNodeIcon;

	/**
	 * Node special icon
	 */
	static protected final ImageIcon theNodeSpecialIcon;

	/**
	 * Edges icon
	 */
	static protected final ImageIcon theEdgesIcon;

	/**
	 * Edge icon
	 */
	static protected final ImageIcon theEdgeIcon;

	/**
	 * Tools icon
	 */
	static protected final ImageIcon theToolsIcon;

	/**
	 * Menu icon
	 */
	static protected final ImageIcon theMenuIcon;

	/**
	 * Default icon
	 */
	static protected final ImageIcon theDefaultIcon;

	// font used

	/**
	 * Default font
	 */
	static protected final Font theDefaultFont;

	/**
	 * Bold font
	 */
	static protected final Font theBoldFont;

	/**
	 * Italic font
	 */
	static protected final Font theItalicFont;

	static
	{
		// icons
		thejFamilyTreeIcon = new ImageIcon(Renderer.class.getResource("images/treehome.png")); //$NON-NLS-1$
		theTreeIcon = new ImageIcon(Renderer.class.getResource("images/treetree.png")); //$NON-NLS-1$
		theNodesIcon = new ImageIcon(Renderer.class.getResource("images/treenodes.png")); //$NON-NLS-1$
		theRootIcon = new ImageIcon(Renderer.class.getResource("images/treeroot.png")); //$NON-NLS-1$
		theNodeIcon = new ImageIcon(Renderer.class.getResource("images/treenode.png")); //$NON-NLS-1$
		theNodeSpecialIcon = new ImageIcon(Renderer.class.getResource("images/treenodespecial.png")); //$NON-NLS-1$
		theEdgesIcon = new ImageIcon(Renderer.class.getResource("images/treeedges.png")); //$NON-NLS-1$
		theEdgeIcon = new ImageIcon(Renderer.class.getResource("images/treeedge.png")); //$NON-NLS-1$
		theToolsIcon = new ImageIcon(Renderer.class.getResource("images/treetools.png")); //$NON-NLS-1$
		theMenuIcon = new ImageIcon(Renderer.class.getResource("images/treemenuitem.png")); //$NON-NLS-1$
		theDefaultIcon = new ImageIcon(Renderer.class.getResource("images/treedefault.png")); //$NON-NLS-1$

		// fonts
		theDefaultFont = new Font("Dialog", Font.PLAIN, 12); //$NON-NLS-1$
		theBoldFont = new Font("Dialog", Font.BOLD, 12); //$NON-NLS-1$
		theItalicFont = new Font("Dialog", Font.ITALIC, 12); //$NON-NLS-1$
	}

	/**
	 * Constructor
	 */
	public Renderer()
	{
		this.setOpenIcon(theDefaultIcon);
		this.setClosedIcon(theDefaultIcon);
		this.setLeafIcon(theNodeIcon);
		this.setBorderSelectionColor(Color.BLUE);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent(javax.swing.JTree, java.lang.Object, boolean, boolean, boolean, int, boolean)
	 */
	@Override
	public Component getTreeCellRendererComponent(JTree tree, Object thisValue, boolean isSelected, boolean expanded, boolean leaf, int row, boolean hasFocus)
	{
		super.getTreeCellRendererComponent(tree, thisValue, isSelected, expanded, leaf, row, hasFocus);
		DefaultMutableTreeNode thisTreeNode = (DefaultMutableTreeNode) thisValue;
		Object thisObject = thisTreeNode.getUserObject();
		if (thisObject instanceof TreeMutableNode)
		{
			Node thisNode = (Node) thisTreeNode.getUserObject();
			String thisLabel = thisNode.getLabel();
			String thisContent = thisNode.getContent();
			boolean isSpecial = thisNode.getLink() != null || thisNode.getMountPoint() != null;
			boolean isRoot = thisNode.getParent() == null;
			setText(thisLabel);
			setIcon(isRoot ? theRootIcon : isSpecial ? theNodeSpecialIcon : theNodeIcon);
			setFont(theDefaultFont);
			setForeground(Color.BLACK);
			setToolTipText(getNodeTooltip(thisLabel, thisContent));
		}
		else if (thisObject instanceof TreeMutableEdge)
		{
			Edge thisEdge = (Edge) thisTreeNode.getUserObject();
			setText(thisEdge.toString());
			setFont(theDefaultFont);
			setIcon(theEdgeIcon);
			setForeground(Color.BLACK);
			setToolTipText(Messages.getString("Renderer.51")); //$NON-NLS-1$
		}
		else if (thisObject instanceof TopWrapper)
		{
			setText(Messages.getString("Renderer.10")); //$NON-NLS-1$
			setFont(theDefaultFont);
			setIcon(thejFamilyTreeIcon);
			setForeground(Color.GRAY);
			setToolTipText(Messages.getString("Renderer.11")); //$NON-NLS-1$
		}
		else if (thisObject instanceof TreeWrapper)
		{
			setText(Messages.getString("Renderer.20")); //$NON-NLS-1$
			setFont(theDefaultFont);
			setIcon(theTreeIcon);
			setForeground(Color.GRAY);
			setToolTipText(Messages.getString("Renderer.21")); //$NON-NLS-1$
		}
		else if (thisObject instanceof NodesWrapper)
		{
			setText(Messages.getString("Renderer.30")); //$NON-NLS-1$
			setFont(theDefaultFont);
			setIcon(theNodesIcon);
			setForeground(Color.GRAY);
			setToolTipText(Messages.getString("Renderer.31")); //$NON-NLS-1$
		}
		else if (thisObject instanceof EdgesWrapper)
		{
			setText(Messages.getString("Renderer.40")); //$NON-NLS-1$
			setFont(theDefaultFont);
			setIcon(theEdgesIcon);
			setForeground(Color.GRAY);
			setToolTipText(Messages.getString("Renderer.41")); //$NON-NLS-1$
		}
		else if (thisObject instanceof ToolsWrapper)
		{
			setText(Messages.getString("Renderer.80")); //$NON-NLS-1$
			setFont(theDefaultFont);
			setIcon(theToolsIcon);
			setForeground(Color.GRAY);
			setToolTipText(Messages.getString("Renderer.81")); //$NON-NLS-1$
		}
		else if (thisObject instanceof MenuWrapper)
		{
			setText(Messages.getString("Renderer.90")); //$NON-NLS-1$
			setFont(theDefaultFont);
			setIcon(theMenuIcon);
			setForeground(Color.GRAY);
			setToolTipText(Messages.getString("Renderer.91")); //$NON-NLS-1$
		}
		else if (thisObject instanceof MenuItemWrapper)
		{
			MenuItemWrapper thisWrapper = (MenuItemWrapper) thisTreeNode.getUserObject();
			MenuItem thisMenuItem = thisWrapper.theMenuItem;
			setText(thisMenuItem.theLabel);
			setFont(theDefaultFont);
			setIcon(theMenuIcon);
			setForeground(Color.BLACK);
			setToolTipText(Messages.getString("Renderer.92")); //$NON-NLS-1$
		}
		else
		{
			setText("?"); //$NON-NLS-1$
			setFont(theDefaultFont);
			setIcon(theDefaultIcon);
			setForeground(Color.GRAY);
			setToolTipText(""); //$NON-NLS-1$
		}

		// selection
		if (isSelected)
		{
			setFont(theBoldFont);
			setForeground(Color.BLACK);
		}
		return this;
	}

	/**
	 * Get node tooltip string
	 * 
	 * @param thisLabel
	 *        node label
	 * @param thisContent
	 *        node content
	 * @return tooltip string
	 */
	private String getNodeTooltip(String thisLabel, String thisContent)
	{
		StringBuffer thisBuffer = new StringBuffer();
		thisBuffer.append("<html><strong>Node</strong><br>"); //$NON-NLS-1$
		thisBuffer.append(thisLabel);
		thisBuffer.append("<br>"); //$NON-NLS-1$
		if (thisContent != null)
		{
			thisBuffer.append("<i>"); //$NON-NLS-1$
			if (thisContent.length() < 16)
				thisBuffer.append(thisContent);
			else
			{
				thisBuffer.append(thisContent.substring(0, 16));
				thisBuffer.append(" ..."); //$NON-NLS-1$
			}
			thisBuffer.append("</i>"); //$NON-NLS-1$
		}
		thisBuffer.append("</html>"); //$NON-NLS-1$
		return thisBuffer.toString();
	}
}